﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using UniMana.Utils.Helper;
using UniMana.Data;
using UniMana.Utils.Controls;
using System.Data;
using System.ComponentModel;
using DevExpress.XtraGrid.Views.Grid;

namespace UniMana.Win.Training
{
    public partial class FrmSubjectRelationship : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_dbTraining = new DBMain();
        private bds m_bds = new bds();
        private int m_iDepartmentKey;
        private int m_iSubjectKey;

        private DataTable m_dtbLeft;
        private DataTable m_dtbRight;


        public FrmSubjectRelationship()
        {
            InitializeComponent();
            //UniMana.Data.SysUtils.SetRuleControl(this.Controls, this.Name); |EDIT|
            LoadData();
        }

        public FrmSubjectRelationship(int _iSubjectKey,int _iDepartmentKey)
        {
            InitializeComponent();
            SetFirstData(_iSubjectKey,_iDepartmentKey);
            LoadData();
        }

        private void LoadData()
        {
            this.rluSubjectType.DataSource = UniMana.Utils.Configs.SubjectType.ToDataTable;
            LoadSubjectRelationShip();
            LoadSubject();
        }

        /// <summary>
        /// Lấy danh sách môn học đưa vào bảng subject
        /// </summary>
        public void LoadSubject()
        {
            this.m_dtbLeft = this.m_dbTraining.m_DCReadOnly.Training_Subjects.GetListByDepartmentKey(this.m_iDepartmentKey).ToDataTable();
            RemoveSubjectLeft();
            this.gctSubject.DataSource = this.m_dtbLeft;
        }

        public void RemoveSubjectLeft()
        {
            for (int i = 0; i < this.m_dtbLeft.Rows.Count; i++)
            {
                int subjectKey = (int)this.m_dtbLeft.Rows[i]["Training_SubjectKey"];
                for (int j = 0; j < this.m_dtbRight.Rows.Count; j++)
                {
                    int subjectKeyRight = (int)this.m_dtbRight.Rows[j]["Training_SubjectKey2"];
                    if (subjectKey == subjectKeyRight)
                    {
                        this.m_dtbLeft.Rows.RemoveAt(i);
                        i--;
                        break;
                    }
                }
                if (subjectKey == this.m_iSubjectKey)
                {
                    this.m_dtbLeft.Rows.RemoveAt(i);
                    i--;
                }
            }
        }

        /// <summary>
        /// Lấy danh sách môn liên quan
        /// </summary>
        public void LoadSubjectRelationShip()
        {
            this.m_dtbRight = this.m_dbTraining.m_DCReadOnly.Training_SubjectRelationships.GetListBySubjectKey(this.m_dbTraining.m_DCReadOnly.Training_Subjects, this.m_iSubjectKey).ToDataTable();
            if (this.m_dtbRight.Columns.Count == 0)
            {
                this.m_dtbRight.Columns.Add("Training_SubjectRelationshipKey");
                this.m_dtbRight.Columns.Add("Training_SubjectKey1");
                this.m_dtbRight.Columns.Add("Training_SubjectKey2");
                this.m_dtbRight.Columns.Add("ID");
                this.m_dtbRight.Columns.Add("Name");
                this.m_dtbRight.Columns.Add("Relationship");
                this.m_dtbRight.Columns.Add("Catalogue_DepartmentKey");
                this.m_dtbRight.Columns.Add("Note");
                this.m_dtbRight.Columns.Add("Credits1");
                this.m_dtbRight.Columns.Add("Credits2");
                this.m_dtbRight.Columns.Add("Credits3");
                this.m_dtbRight.Columns.Add("Credits4");
                this.m_dtbRight.Columns.Add("TotalCredits");
            }
            this.gctTrainingProgram_SubjectType.DataSource = this.m_dtbRight;
        }

        /// <summary>
        /// Sét dữ liệu ban đầu
        /// </summary>
        /// <param name="_iSubjectKey">Mã môn học</param>
        /// <param name="_strSubjectID">ID Môn</param>
        /// <param name="_strSubjectName">Tên Môn</param>
        /// <param name="_iTrainingProgramKey">Mã CTĐT</param>
        /// <param name="_strTrainingProgramID">ID CTĐT</param>
        /// <param name="_strTrainingProgramName">Tên CTĐT</param>
        public void SetFirstData(int _iSubjectKey,int _iDepartmentKey)
        {
            this.m_iSubjectKey = _iSubjectKey;
            this.m_iDepartmentKey = _iDepartmentKey;
            Training_SubjectInfo subjectInfo = new Training_SubjectInfo(_iSubjectKey);

            this.txtSubjectID.Text = subjectInfo.Properties.ID;
            this.txtSubjectName.Text = subjectInfo.Properties.Name;
        }

        /// <summary>
        /// Đưa môn học vào liên quan
        /// </summary>
        public void AddSubject()
        {
            if (this.grvSubject.RowCount > 0)
            {
                if (this.grvSubject.FocusedRowHandle >= 0)
                {
                    if (!this.grvSubject.IsFilterRow(this.grvSubject.FocusedRowHandle))
                    {
                        int subjectKey2 = Convert.ToInt16( this.grvSubject.GetFocusedRowCellValue(gclSubjectKey).ToString() );
                        int subjectKey1 = this.m_iSubjectKey;
                        string subject2ID = this.grvSubject.GetFocusedRowCellValue(gclSubjectID_Left).ToString();
                        string subject2Name = this.grvSubject.GetFocusedRowCellValue(gclSubjectName_Left).ToString();
                        Training_SubjectRelationshipInfo info = new Training_SubjectRelationshipInfo(subjectKey1, subjectKey2);
                        if (info.Properties.Relationship == null)
                        {
                            info.Save();
                            for (int i = 0; i < this.m_dtbLeft.Rows.Count; i++)
                            {
                                if (this.m_dtbLeft.Rows[i]["Training_SubjectKey"].ToString() == subjectKey2.ToString())
                                {
                                    this.m_dtbLeft.Rows.RemoveAt(i);
                                    break;
                                }
                            }
                            LoadSubjectRelationShip();
                            this.grcLeft.Enabled = false;
                            this.btnAdd.Enabled = false;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Xóa môn học liên quan
        /// </summary>
        public void RemoveSubject()
        {
            if (this.grvTrainingProgram_SubjectType.RowCount > 0)
            {
                if (this.grvTrainingProgram_SubjectType.FocusedRowHandle >= 0)
                {
                    DialogResult dialogResult = this.ShowConfirm();
                    if (dialogResult == DialogResult.Yes)
                    {
                        int subjectKeyRelationShip = Convert.ToInt16( this.grvTrainingProgram_SubjectType.GetFocusedRowCellValue(gclTraining_SubjectRelationshipKey).ToString() );
                        Training_SubjectRelationshipInfo info = new Training_SubjectRelationshipInfo(subjectKeyRelationShip);
                        if (info.ExistInDataBase)
                        {
                            info.Delete();
                            info.Save();
                            this.grvTrainingProgram_SubjectType.DeleteSelectedRows();
                            LoadSubject();
                        }
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddSubject();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            RemoveSubject();
            LoadSubject();
        }


        /// <summary>
        /// Lưu khi chọn môn liên quan
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grvTrainingProgram_SubjectType_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column == gclType)
            {
                if (e.Value != null)
                {
                    if (e.Value.ToString() != "0")
                    {
                        int type = Convert.ToInt16(e.Value.ToString());
                        int subjectKey1 = this.m_iSubjectKey;
                        int subjectKey2 = Convert.ToInt16(this.grvTrainingProgram_SubjectType.GetFocusedRowCellValue(gclTraining_SubjectRelationshipKey).ToString());
                        int relationShipKey = Convert.ToInt16(this.grvTrainingProgram_SubjectType.GetFocusedRowCellValue(gclTraining_SubjectRelationshipKey).ToString());
                        Training_SubjectRelationshipInfo info = new Training_SubjectRelationshipInfo(relationShipKey);
                        if (info.ExistInDataBase)
                        {
                            info.Properties.Relationship = type;
                            info.Save();
                            this.grcLeft.Enabled = true;
                            this.btnAdd.Enabled = true;
                        }
                      
                    }
                }
            }
        }

        private void grvTrainingProgram_SubjectType_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = sender as GridView;
            if (grdView.FocusedColumn != gclType)
            {
                e.Cancel = true;
            }
        }

        //private bool m_bCheckRequire = true;
        //private void CheckRequire(int _iTrainingProgramKey, int _iSubjectKey1, int _iSubjectKey2)
        //{
        //    DataTable dt = this.m_dbTraining.m_DCReadOnly.Training_TrainingProgram_SubjectTypes.GetListSubjectRequire(_iTrainingProgramKey, _iSubjectKey2).ToDataTable();
        //    if (dt.Rows.Count > 0)
        //    {
        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            if (Convert.ToInt16(dt.Rows[i]["Training_SubjectKey2"].ToString()) == _iSubjectKey1)
        //            {
        //                this.m_bCheckRequire = true;
        //                return;
        //            }
        //            CheckRequire(_iTrainingProgramKey, _iSubjectKey1, Convert.ToInt16(dt.Rows[i]["Training_SubjectKey2"].ToString()));
        //        }
        //    }
        //}
    }
}